package com.ikdong.toodle.widget;

import java.util.ArrayList;
import java.util.List;
import com.ikdong.toodle.R;
import com.ikdong.toodle.model.Folder;
import com.ikdong.toodle.util.Constant;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FolderListAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<Folder> folders = new ArrayList<Folder>();
	private Context context;
	
	public FolderListAdapter(Context context){
		this.context = context;
		inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return folders.size();
	}

	@Override
	public Folder getItem(int position) {
		return folders.get(position);
	}

	@Override
	public long getItemId(int position) {
		return folders.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView != null ? convertView : inflater.inflate(R.layout.folder_list_item,parent,false);
		final Folder folder = this.getItem(position);
		TextView name = (TextView) view.findViewById(R.id.fli_folder);
		TextView privated = (TextView) view.findViewById(R.id.fli_private);
		TextView archived = (TextView) view.findViewById(R.id.fli_archived);
		ImageView icon = (ImageView) view.findViewById(R.id.fli_private_icon);
		
		name.setText(folder.getName());
		if(Constant.FOLDER_STATUS_PRIVATE.equals(folder.getPrivated())){
			privated.setVisibility(View.VISIBLE);
			icon.setVisibility(View.VISIBLE);
		}else{
			privated.setVisibility(View.INVISIBLE);
			icon.setVisibility(View.INVISIBLE);
		}
		if(Constant.FOLDER_STATUS_ARCHIVED.equals(folder.getArchived())){
			archived.setVisibility(View.VISIBLE);
		}else{
			archived.setVisibility(View.INVISIBLE);
		}
		
		return view;
	}
	
	public void removeItem(long id){
		for(int i=0; i<folders.size(); i++){
			if(folders.get(i).getId()==id){
				folders.remove(i);
				this.notifyDataSetChanged();
				return;
			}
		}
	}

	public List<Folder> getFolders() {
		return folders;
	}

	public void setFolders(List<Folder> folders) {
		this.folders.clear();
		this.folders.addAll(folders);
	}

}
